package it.rikesh.aip2011.dataloading;

import it.rikesh.aip2011.shared.model.Hut;
import it.rikesh.aip2011.shared.model.HutTrail;
import it.rikesh.aip2011.shared.model.Trail;

import java.io.InputStream;
import java.util.Stack;

import javax.servlet.ServletContext;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.slim3.datastore.Datastore;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.Text;

/**
 * @author  RN
 */
public class TrailHandler extends DefaultHandler{
    private Stack<Trail> trailStack;
    private String characters;
    private String[] huts;
    
    public TrailHandler(ServletContext context){
        SAXParserFactory factory3 = SAXParserFactory.newInstance();
        try {
            SAXParser saxParser = factory3.newSAXParser();
            InputStream is = context.getResourceAsStream("/data/trail.xml");
            saxParser.parse(is, this);
        } catch (Throwable t) {
            t.printStackTrace();

        }
    }
    
    public void startDocument() 
    throws SAXException {

        trailStack = new Stack<Trail>();

    }

    public void startElement(String namespaceURI, 
            String localName,
            String qualifiedName,
            Attributes attributes) 
    throws SAXException {

        if (qualifiedName.equals("trail")) {

            Trail trail = new Trail();
            Key key = Datastore.createKey(Trail.class, Long.parseLong(attributes.getValue("id")));
            trail.setKey(key);
            trailStack.push(trail);

        }

    }

    public void endElement(String namespaceURI, 
            String simpleName,
            String qualifiedName) 
    throws SAXException {

        if (!trailStack.isEmpty()) {

            if (qualifiedName.equals("trail")) {
                Trail trail=trailStack.pop();
                Datastore.put(trail);
                Hut hutter;
                for (String hut : huts) {
                    Key key=Datastore.createKey(Hut.class, Long.parseLong(hut));
                    hutter = Datastore.get(Hut.class, key);
                    HutTrail ht=new HutTrail();
                    ht.getHutRef().setModel(hutter);
                    ht.getTrailRef().setModel(trail);
                    Datastore.put(ht);
                }
                
            } 
            else if (qualifiedName.equals("name")) {

                Trail trail = trailStack.pop();
                trail.setName(characters);
                trailStack.push(trail);

            } 
            else if (qualifiedName.equals("ddescription")) {

                Trail trail = trailStack.pop();
                trail.setDDescription(new Text(characters));
                trailStack.push(trail);

            }
            else if (qualifiedName.equals("link")) {

                Trail trail = trailStack.pop();
                trail.setLink(characters.replace("#", "&"));
                trailStack.push(trail);

            }
            else if (qualifiedName.equals("huts")) {

                huts=characters.split(";");
                
            }
            else if (qualifiedName.equals("activity")) {

                Trail trail = trailStack.pop();
                trail.setActivity(characters);
                trailStack.push(trail);
                
            }
        }

    }

    public void characters(char buf[], int offset, int len) 
    throws SAXException {

        characters = new String(buf, offset, len);

    }
}
